<template>
  <div class="main">
    <section>
      <div class="btn-block">
        <el-button v-if="rightsButtons['add']" type="success" size="small" icon="el-icon-circle-plus" plain @click="handleAddOrEdit()">新建云仓转换</el-button>
      </div>
      <el-table :data="list" v-loading="listLoading" header-row-class-name="table-header" element-loading-text="给我一点时间" border highlight-current-row>
        <el-table-column align="center" label="云仓活动名称" prop="fromSeriesName" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="转换云仓活动名称" prop="toSeriesName" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" label="转换比例" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{scope.row.fromNumber}} : {{scope.row.toNumber}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="160" v-if="rightsButtons['edit'] || rightsButtons['del']">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" plain @click="handleAddOrEdit(scope.row)" v-if="rightsButtons['edit']">编辑</el-button>
            <el-button type="danger" size="mini" plain @click="handleDel(scope.row.id)" v-if="rightsButtons['del']">移除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="listQuery.pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="listQuery.pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="listTotal">
      </el-pagination>
    </section>
    <el-dialog :title="formData.id?'编辑':'新增'" :visible.sync="addOrEditDialog">
      <el-form :model="formData" ref="formData" label-width="150px">
        <el-form-item prop="fromSeriesId" label="选择云仓活动" :rules="commonRules.notNullRule">
          <el-select v-model="formData.fromSeriesId" placeholder="选择云仓活动" clearable>
            <el-option v-for="item in activityList" :key="item.seriesId" :label="item.seriesName" :value="item.seriesId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="toSeriesId" label="选择要转换云仓活动" :rules="commonRules.notNullRule">
          <el-select v-model="formData.toSeriesId" placeholder="选择要转换云仓活动" clearable>
            <el-option v-for="item in activityList" :key="item.seriesId" :label="item.seriesName" :value="item.seriesId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="转换模式" :rules="commonRules.notNullRule">
          <el-radio-group v-model="formData.type" @change="handleChange">
            <el-radio :label="1">1:N</el-radio>
            <el-radio :label="2">N :1</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="转换比例" :rules="commonRules.notNullRule">
          <el-row>
            <el-col :span="6">
              <el-input v-model="formData.fromNumber" :disabled="formData.type===1" @input="formData.fromNumber=$event.replace(/[^\d]/g,'')" placeholder="源数量"></el-input>
            </el-col>
            <el-col :span="2" style="text-align: center">
              ：
            </el-col>
            <el-col :span="6">
              <el-input v-model="formData.toNumber" :disabled="formData.type===2" @input="formData.toNumber=$event.replace(/[^\d]/g,'')" placeholder="目标数量"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="btnLoading" @click="save('formData')">确定</el-button>
        <el-button @click="addOrEditDialog = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'yuncangChange',
    data() {
      return {
        activityList: [],
        btnLoading: false,
        formData: {
          id: '',
          fromSeriesId: '',
          toSeriesId: '',
          fromNumber: 1,
          toNumber: null,
          type: 1
        },
        addOrEditDialog: false,
        listLoading: false,
        listQuery: {
          pageIndex: 1,
          pageSize: 10
        },
        list: [],
        listTotal: 0
      }
    },
    created() {
      if (this.$store.getters.cachedViews.length === 0) {
        this.getList()
        this.getActivity()
      }
    },
    activated() {
      this.getList()
      this.getActivity()
    },
    methods: {
      handleChange (val) {
        if (this.formData.type === 1) {
          this.formData.fromNumber = 1
          this.formData.toNumber = null
        } else {
          this.formData.fromNumber = null
          this.formData.toNumber = 1
        }
      },
      // 获取云仓活动下拉列表
      getActivity () {
        this.request({
          url: '/bsnl-product/box/list/cloudStock',
          method: 'get',
        }).then((res) => {
          if (res.status === 200) {
            this.activityList = res.data
          }
        }).catch(err => {
         console.log(err)
         this.activityList = []
        })
      },
      save (name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            if (this.formData.type === 2 && !this.formData.fromNumber) {
              this.$message.error('原数量必填')
              return
            }
            if (this.formData.type === 1 && !this.formData.toNumber) {
              this.$message.error('目标数量必填')
              return
            }
            if (this.formData.type === 1 && !(Number(this.formData.toNumber) >= 1)) {
              this.$message.error('目标数量必须是大于等于1的正整数')
              return
            }
            if (this.formData.type === 2 && !(Number(this.formData.fromNumber) >= 1)) {
              this.$message.error('原数量必须是大于等于1的正整数')
              return
            }
            this.btnLoading = true
            this.request({
              url: '/bsnl-product/box/stockExchange/update',
              method: 'post',
              data: { 
                ...this.formData
              }
            }).then((res) => {
              if (res.status === 200) {
                this.btnLoading = false
                this.$message.success(res.message)
                this.addOrEditDialog = false
                this.getList()
              }
            }).catch(err => {
              this.btnLoading = false
              console.log(err)
            })
          }
        })
      },
      // 新增编辑云仓库存转换
      handleAddOrEdit (row) {
        this.addOrEditDialog = true
        if (row) {
          this.formData = Object.assign({}, row)
        } else {
          this.formData = {
            id: '',
            fromSeriesId: '',
            toSeriesId: '',
            fromNumber: 1,
            toNumber: null,
            type: 1
          }
          this.$nextTick(() => {
            this.$refs.formData.clearValidate()
          })
        }
      },
      // 移除
      handleDel (id) {
        this.$confirm('移除后将删除此转换规则?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.request({
            url: '/bsnl-product/box/stockExchange/delete',
            method: 'get',
            params: { 
              boxStockExchangeId: id
            }
          }).then((res) => {
            if (res.status === 200) {
              this.$message.success('移除成功')
              this.getList()
            }
          }).catch(err => {
            console.log(err)
          })
        }).catch((e) => {
          console.error(e);
        })
      },
      // 获取列表
      getList() {
        this.listLoading = true
        this.request({
          url: '/bsnl-product/box/stockExchange/list',
          method: 'post',
          data: { ...this.listQuery }
        }).then((res) => {
          this.list = res.data
          this.listTotal = res.total
          this.listLoading = false
        }).catch(err => {
          console.log(err)
          this.list = []
          this.listTotal = 0
          this.listLoading = false
        })
      },
      handleSizeChange (value) {
        this.listQuery.pageSize = value
        this.getList()
      },
      handleCurrentChange(val){
        this.listQuery.pageIndex = val
        this.getList()
      }
    }
  }
</script>
<style lang="scss" scoped>
.title {
  font-size: 16px;
  margin-bottom: 20px;
  border-bottom: 1px solid #eee;
  padding: 0 0 10px 15px;
}
</style>